package com.example.swim.controller;


import com.example.swim.Pojo.Apply;
import com.example.swim.Pojo.Student;
import com.example.swim.Pojo.VIP;
import com.example.swim.Service.*;
import com.example.swim.Utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @author LiYa
 * @create 2022-08-01 9:51
 */
@RestController
@RequestMapping("/join")
public class JoinController {
    @Autowired
    AcService acService;
    @Autowired
    ClassRoomService classRoomService;
    @Autowired
    StudentService studentService;
    @Autowired
    VipService vipService;
    @Autowired
    ApplyService applyService;
    @RequestMapping("/Add")
    public R addActivity(@RequestParam HashMap<String,Object> map)
    {
        int aid=Integer.parseInt((String) map.get("aid"));
        int id=Integer.parseInt((String) map.get("user[id]"));
        VIP vip=vipService.findVIP(id);
        if (aid == (vip.getAid()==null?0:vip.getAid())) {
            return new R(true, "已经报名，无需重复报名");
        }
        acService.addNum(aid);
        vipService.addAid(aid,id);
        if(vip.getGrade()==1){
            return new R(true,"会员报名成功，线下支付报名金额");
        }
        else
        {
            return new R(true,"超级会员无需支付,成功报名");
        }
    }
    @RequestMapping("/SAdd")
    public R Sadd(@RequestParam HashMap<String,Object> map){
        int aid=Integer.parseInt((String) map.get("aid"));
        int id=Integer.parseInt((String) map.get("user[id]"));
        Student student=studentService.selectById(id);
        if(aid==(student.getAid()==null?0:student.getAid()))
        {
            return new R(true,"已经报名，无需重复报名");
        }
        acService.addNum(aid);
        studentService.addAid(aid,id);
        return new R(true,"学员报名成功啦");
    }
//    班级报名
    @RequestMapping("/apply")
    public String apply(@RequestParam Integer cid, Model model)
    {
        model.addAttribute("cid", cid);
        return"application";
    }
//    班级报名分种类
    @RequestMapping("/finish")
    public R finish(Apply apply)
    {

        if(classRoomService.findAge(apply.getCid()).equals("儿童")&&apply.getAge()<18
        ||classRoomService.findAge(apply.getCid()).equals("成人")&&apply.getAge()>=18)
        {
            applyService.insert(apply);
            classRoomService.updateNum(apply.getCid());
            return new R(true, "报名成功，后续通知将通过邮箱发出，亲留意邮箱消息");
        }
        else
        {
            return new R(false,"年龄不符合该班级，无法报名");
        }
    }

////    判断学员是否有课程，如果有课程返回false，如果没有课程则返回true，并报名成功
//    @GetMapping("/checkS")
//    public R checkS(int id,int cid)
//    {
//        R r=studentService.Check(id, cid);
//       if(r.getFlag())
//       {
//           classRoomService.updateNum(cid);
//       }
//       return r;
//    }
//购买会员卡
    @GetMapping("/buyCard")
    public R buy(Integer id,Integer cid)
    {
        vipService.buyCard(id,cid);
        VIP vip=vipService.findVIP(id);
        HashMap<String,Object> map=new HashMap<>();
        map.put("user", vip);
        map.put("identity", "vip");
        return new R(true,map);
    }
}
